﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Cms.Model;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;

namespace Portal.Cms.Bussiness
{
    public class DocReleaseFromProcessBll
    {
        public static DocReleaseFromCls CreateDocReleaseFromModel(string SiteId, string DocReleaseFromId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableDocReleaseFrom " +
                                               "     where DocReleaseFromId=@DocReleaseFromId order by SortIndex",
                    new DbParam[] 
                    { 
                        new DbParam("DocReleaseFromId", DocReleaseFromId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            DocReleaseFromCls ODocReleaseFrom = DocReleaseFromParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return ODocReleaseFrom;
        }



        public static DocReleaseFromCls[] ReadingDocReleaseFrom(string SiteId, string GroupDataId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TableDocReleaseFrom where frkGroupDataId=@frkGroupDataId order by DocReleaseFromName", new DbParam[]{
                                                new DbParam("frkGroupDataId", GroupDataId)});

            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new DocReleaseFromCls[0];
            }
            DocReleaseFromCls[] DocReleaseFrom = DocReleaseFromParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return DocReleaseFrom;
        }


        public static void AddDocReleaseFrom(string SiteId, string GroupDataId, string DocReleaseFromName, string SortIndex)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DocReleaseFromId = System.Guid.NewGuid().ToString();
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DBService.Insert(null, "TableDocReleaseFrom",
                    new DbParam[] 
                    { 
                        new DbParam("DocReleaseFromId", DocReleaseFromId),
                        new DbParam("frkGroupDataId", GroupDataId),
                        new DbParam("DocReleaseFromName", DocReleaseFromName),
                        new DbParam("SortIndex", SortIndex),
                        new DbParam("AssetCode", AssetCode),
                        new DbParam("DataLanguage", DataLanguage),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Thêm mới Cơ quan ban hành " + DocReleaseFromName,
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName,
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }


        public static void SaveDocReleaseFrom(string SiteId, string DocReleaseFromId, string DocReleaseFromName, string SortIndex)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DBService.Update(null, "TableDocReleaseFrom", "DocReleaseFromId", DocReleaseFromId,
                    new DbParam[] 
                    { 
                        new DbParam("DocReleaseFromName", DocReleaseFromName),
                        new DbParam("SortIndex", SortIndex),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Sửa chữa Cơ quan ban hành " + DocReleaseFromName,
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName,
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }


        public static void DeleteDocReleaseFrom(string SiteId, string DocReleaseFromId)
        {
            DocReleaseFromCls ODocReleaseFrom = DocReleaseFromProcessBll.CreateDocReleaseFromModel(SiteId, DocReleaseFromId);
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query =
                " Delete from TableDocReleaseFrom where DocReleaseFromId=@DocReleaseFromId; ";
            DBService.ExecuteNonQuery(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("DocReleaseFromId", DocReleaseFromId),
                    });
            SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddSysLogProcess(SiteId,
                WebSessionUtility.GetCurrentLoginUser().AssetCode,
                "Dictionary", "Xóa Cơ quan ban hành " + ODocReleaseFrom.DocReleaseFromName,
                WebSessionUtility.GetCurrentLoginUser().UserId,
                WebSessionUtility.GetCurrentLoginUser().LoginName,
                WebSessionUtility.GetCurrentLoginUser().FullName);
        }
    }
}
